package GameTools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.json.JSONObject;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.Log;

public class ydPoint implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1394253087836345804L;
	public float x;
	public float y;

	Paint p = new Paint();

	public ydPoint() {
		x = 0;
		y = 0;
	}

	public ydPoint(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public ydPoint(ydPoint p) {
		this.x = p.x;
		this.y = p.y;
	}

	public void rotate(ydPoint pivot, float rads) {

		double cosVal = Math.cos(rads);
		double sinVal = Math.sin(rads);

		ydPoint tmp = new ydPoint(x, y);

		tmp.x -= pivot.x;
		tmp.y -= pivot.y;

		x = (float) (tmp.x * cosVal - tmp.y * sinVal) + pivot.x;
		y = (float) (tmp.x * sinVal + tmp.y * cosVal) + pivot.y;

		return;

	}

	public String toString() {
		return x + " " + y;
	}

	public void degreesToVector(float slopeDegrees) {

		float extra = 0;

		if (slopeDegrees >= 270)
			extra = 270;
		else if (slopeDegrees >= 180)
			extra = 180;
		else if (slopeDegrees >= 90)
			extra = 90;
		else
			extra = 0;

		slopeDegrees -= extra;

		x = (float) Math.cos(ydMath.degreesToRads(slopeDegrees));
		y = (float) Math.sin(ydMath.degreesToRads(slopeDegrees));

		if ((int) extra == 270) {
			y *= 1;
			float tmp = x;
			x = y;
			y = -tmp;
		} else if ((int) extra == 180) {
			x *= -1;
			y *= -1;
		} else if ((int) extra == 90) {
			x *= 1;
			y *= -1;
			float tmp = x;
			x = y;
			y = tmp;
		} else {
			y *= 1;
		}

		y *= -1;
	}

	public void drawPoint(Canvas canvas) {
		p.setColor(Color.GREEN);
		canvas.drawPoint((int) x, (int) y, p);
	}

	public void normailize() {
		float h = (float) Math.hypot(x, y);

		x /= h;
		y /= h;
	}

	public void deltaMove(ydPoint shift) {
		x += shift.x;
		y += shift.y;

	}

	public static ydPoint stringPairToydPoint(String optString) {
		StringTokenizer st = new StringTokenizer(optString, ",");
		return new ydPoint(Float.parseFloat(st.nextToken()),
				Float.parseFloat(st.nextToken()));

	}

	public void setCoordinates(ydPoint newCoor) {
		x = newCoor.x;
		y = newCoor.y;

	}

	public void setCoordinates(float x, float y) {
		this.x = x;
		this.y = y;

	}

	public static ArrayList<ydPoint> jsonObjectToArrayList(JSONObject jsonObject) {
		ArrayList<ydPoint> points = new ArrayList<ydPoint>();

		int i = 0;
		while (jsonObject.has(i + "")) {
			points.add(stringPairToydPoint(jsonObject.optString(i + "")));
			Log.d("LOADING", i + " CONVERTING TO POINT");
			i++;
		}

		return points;
	}

	public void reverseDirection() {
		x = x * -1;
		y = y * -1;
	}

	public static String arrayToString(ArrayList<ydPoint> coordinates) {

		String str = "";

		for (ydPoint p : coordinates) {
			if (str.compareTo("") != 0)
				str += "<>";
			str += p.x + "," + p.y + "";
		}

		return str;
	}

	public static ArrayList<ydPoint> StringToarray(String str) {

		ArrayList<ydPoint> coordinates = new ArrayList<ydPoint>();
		String[] points = str.split("<>");
		for (int x = 0; x < points.length; x++) {
			coordinates.add(ydPoint.stringPairToydPoint(points[x]));
		}

		return coordinates;
	}

	public static Path ArrayToPath(ArrayList<ydPoint> coordinates) {
		Path p = new Path();
		for (ydPoint point : coordinates) {
			if (p.isEmpty())
				p.moveTo(point.x, point.y);
			else
				p.lineTo(point.x, point.y);
		}
		return p;
	}

	public static ArrayList<ydPoint> PathToArray(Path path) {
		int detail = 100;

		ArrayList<ydPoint> points = new ArrayList<ydPoint>();
		PathMeasure pm = new PathMeasure(path, false);
		detail = (int) pm.getLength();
		// coordinates will be here
		float aCoordinates[] = { 0f, 0f };

		for (int i = 0; i < detail; i++) {
			pm.getPosTan(pm.getLength() * i / detail, aCoordinates, null);
			points.add(new ydPoint(aCoordinates[0], aCoordinates[1]));

		}

		return points;
	}

}
